package genetic.q4.functions;

import genetic.Function;
import genetic.TreeNode;
import genetic.q4.TicTacToeBoard;

public class TryThree extends TicTacToeFunction{

	static int[][] threes =
		new int[][]{
		{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0},
		{3, 4, 5}, {3, 5, 4}, {4, 3, 5}, {4, 5, 3}, {5, 3, 4}, {5, 4, 3},
		{6, 7, 8}, {6, 8, 7}, {7, 6, 8}, {7, 8, 6}, {8, 6, 7}, {8, 7, 6},
		{0, 3, 6}, {0, 6, 3}, {3, 0, 6}, {3, 6, 0}, {6, 0, 3}, {6, 3, 0},
		{1, 4, 7}, {1, 7, 4}, {4, 1, 7}, {4, 7, 1}, {7, 1, 4}, {7, 4, 1},
		{2, 5, 8}, {2, 8, 5}, {5, 2, 8}, {5, 8, 2}, {8, 2, 5}, {8, 5, 2},
		{0, 4, 8}, {0, 8, 4}, {4, 0, 8}, {4, 8, 0}, {8, 0, 4}, {8, 4, 0},
		{2, 4, 6}, {2, 6, 4}, {4, 2, 6}, {4, 6, 2}, {6, 2, 4}, {6, 4, 2}
	};

	int x;
	int y;
	int z;
	/**
	 * constructor for prototype only (NOT TO BE USED)
	 */
	public TryThree() {
		super(null);
	}

	public TryThree(int x, int y, int z,TreeNode doElse){
		super(new TreeNode[]{doElse});
		this.x = x;
		this.y = y;
		this.z = z;
	}

	@Override
	public Function createInstance(TreeNode[] kids) {
		return new TryThree(x, y, z, kids[0]);
	}

	@Override
	public int getNumKids() {
		return 1;
	}

	@Override
	public TreeNode createRandomInstance(TreeNode[] kids) {
		int rand = (int)(Math.random()*threes.length);
		return new TryThree(threes[rand][0], threes[rand][1], threes[rand][2], kids[0]);
	}

	@Override
	public void play(TicTacToeBoard board, int side) {
		if (board.put(side, x)) return;
		else if (board.put(side, y)) return;
		else if (board.put(side, z)) return;
		else if (kids[0].isFunction())
			((TicTacToeFunction)kids[0]).play(board, side);
		else
			((TicTacToeTerminal)kids[0]).play(board, side);


	}

	@Override
	public String getName() {
		return "tt"+x+y+z;
	}

}
